package cn.stronglink.asset.message.mq.dam.handle;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.asset.message.mq.dam.IMessageHandle;
import cn.stronglink.asset.message.mq.dam.entity.ScreenRequertMessageFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;

/**
 * 消息处理上下文
 * 
 * @author yuzhantao
 *
 */
public class MessangHandleContext implements IMessageHandle<MQMessageOfDAM> {
	protected final static String ACTION_CODE="screenRequest";

	protected List<IMessageHandle<MQMessageOfDAM>> handleList = new ArrayList<>();

	/**
	 * 添加消息处理类
	 * 
	 * @param handle
	 */
	public void addHandle(IMessageHandle<MQMessageOfDAM> handle) {
		this.handleList.add(handle);
	}

	/**
	 * 处理消息
	 */
	@Override
	public void handle(MQMessageOfDAM msg) {
		if(ACTION_CODE.equals(msg.getActioncode())) {
			List<MQMessageOfDAM> msgList = ScreenRequertMessageFactory.parse(msg);
			for(int i=0;i<msgList.size();i++) {
				this.handleMsg(msgList.get(i));
			}
		}else {
			this.handleMsg(msg);
		}
	}
	
	private void handleMsg(MQMessageOfDAM msg) {
		for (int i = 0; i < this.handleList.size(); i++) {
			if (this.handleList.get(i).isHandle(msg)) {
				this.handleList.get(i).handle(msg);
				break;
			}
		}
	}

	@Override
	public boolean isHandle(MQMessageOfDAM msg) {
		return true;
	}
}
